package com.naxiang.cn.naxiangbackcard;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.naxiang.cn.adapter.CardItemAdapter;
import com.naxiang.cn.adapter.ItemsBeen;
import com.naxiang.cn.weight.CardView;
import com.naxiang.cn.weight.ToastMaked;
import com.naxiang.cn.weight.WithdrawCash;
import com.squareup.picasso.Picasso;
import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/8/7 14:47
 * 描述:
 */
public class BackCard extends UZModule {

    private CardView        mCardView;
    private Dialog          mItemList;
    private ListView        mCardItems;
    private CardItemAdapter mCardItemAdapter;
    private JSONArray       mListItems;
    private List<ItemsBeen> mList;
    private UZModuleContext mModuleContext;
    private View            mDialogView;
    private View            mFootview;
    private WithdrawCash    mCash;
    private Dialog          mCashDialog;
    private View            mCashDialogView;
    private ListView        mCashCardItems;
    private View            mCashFootview;
    private List<ItemsBeen> mCashList;
    private Dialog          mKeyBardDialog;
    private View            mKeyBardView;

    private RelativeLayout mCloseKeybard;
    private Button         mOne;
    private Button         mTwo;
    private Button         mThree;
    private Button         mFour;
    private Button         mFive;
    private Button         mSix;
    private Button         mSeven;
    private Button         mEnghit;
    private Button         mNine;
    private Button         mTen;
    private Button         mEleven;
    private LinearLayout   mTwlelve;
    private JSONArray      mkeyItems;
    private List<String>   mKey;

    private Handler mHandler = new Handler(Looper.getMainLooper());
    private ImageView mDeleatImage;

    public BackCard(UZWebView webView) {
        super(webView);
    }

    //显示银行卡
    public void jsmethod_showWithdraw(final UZModuleContext moduleContext) {
        mModuleContext = moduleContext;
        String mIndex = moduleContext.optString("index", "");
        boolean mMoved = moduleContext.optBoolean("moved", true);
        if (mCardView == null) {
            mCardView = new CardView(getContext());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.setMargins(0, 0, 0, 0);
            mCardView.setBackgroundResource(R.drawable.backcardbg);
            insertViewToCurWindow(mCardView, params, mIndex, mMoved);
        }

        payForMoneyBack(moduleContext);
    }

    private void payForMoneyBack(final UZModuleContext moduleContext) {
        final String mId = moduleContext.optString("mId", "-1");
        String mCardType = moduleContext.optString("cardtype");
        mListItems = moduleContext.optJSONArray("listItems");
        String mBankIcon = moduleContext.optString("bankicon");
        //加载图片
        Picasso.with(getContext())
                .load(mBankIcon)
                //占位图，图片加载出来之前显示的默认图片
                .placeholder(R.drawable.loading)
                //错误图，图片加载出错时显示的图片
                .error(R.drawable.loading)
                .into(mCardView.getBankIcon());

        mCardView.getCardType().setText(mCardType);
        //点击开始充值
        mCardView.getStartChage().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String money = mCardView.getChargeMoney().getText().toString();

                if (TextUtils.isEmpty(money)) {
                    ToastMaked.showToast(getContext(), "充值金额不能为空");
                    return;
                }

                double chagerMoney = Double.parseDouble(money);
                if (chagerMoney <= 0.0) {
                    ToastMaked.showToast(getContext(), "充值金额不能为0元");
                    return;
                }

                //如果输入的数字末尾是以.结束的，要补全并显示，光标要移动到最后
                if (money.endsWith(".")) {
                    StringBuffer buffer = new StringBuffer(money);
                    buffer.append("0");
                    mCardView.getChargeMoney().setText(buffer.toString());
                    ToastMaked.showToast(getContext(), "请再次核对金额");
                    SystemClock.sleep(100);
                    mCardView.getChargeMoney().setSelection(mCardView.getChargeMoney().getText().length());
                    return;
                }


                JSONObject object = new JSONObject();
                try {
                    object.put("money", money);
                    object.put("mId", mId);
                    moduleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        //点击更换卡片
        mCardView.getChengecard().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("showCardItems", true);
                    moduleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                initDialg();
            }
        });
    }

    private void initDialg() {
        if (mList == null) {
            mList = new ArrayList<>();
        } else {
            mList.clear();
        }
        if (mListItems != null && mListItems.length() != 0) {
            for (int i = 0; i < mListItems.length(); i++) {
                try {
                    String Json = (String) mListItems.get(i);
                    String newJson = Json.replace("'", "\"");
                    JSONObject jsonObject = new JSONObject(newJson);
                    String url = jsonObject.optString("url");
                    String text = jsonObject.optString("text");
                    String mId = jsonObject.optString("mId");
                    boolean select = jsonObject.optBoolean("select");
                    ItemsBeen been = new ItemsBeen();
                    been.setSelect(select);
                    been.setUrl(url);
                    been.setText(text);
                    been.setId(mId);
                    mList.add(been);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        if (mItemList == null) {
            mItemList = new Dialog(getContext(), R.style.items);
            //去除标题栏
            mItemList.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //2.填充布局
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mDialogView = inflater.inflate(R.layout.mo_naxiangbackcard_items_layout, null);
            //将自定义布局设置进去
            mItemList.setContentView(mDialogView);

            //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = mItemList.getWindow();
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;

            //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
            mItemList.show();
            window.setAttributes(lp);


            //对话框消失的监听，主要监听回车键的监听
            mItemList.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("dialogeDismiss", true);
                        mModuleContext.success(object, false);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });

        } else {
            mItemList.show();
        }


        //设置adapter适配器
        mCardItems = (ListView) mDialogView.findViewById(R.id.carditems);

        if (mFootview == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mFootview = inflater.inflate(R.layout.mo_naxiangbackcard_foot_layout, null);
            mCardItems.addFooterView(mFootview);

        }
        mFootview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemList.hide();
                JSONObject object = new JSONObject();
                try {
                    object.put("and", true);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        mCardItemAdapter = new CardItemAdapter(mList);
        mCardItems.setAdapter(mCardItemAdapter);

        //设置条目点击事件
        mCardItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                mItemList.hide();
                JSONObject object = new JSONObject();
                try {
                    object.put("mId", mList.get(position).getId());
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    //隐藏银行卡
    public void jsmethod_hideWithdraw(UZModuleContext moduleContext) {
        if (mCardView != null) {
            removeViewFromCurWindow(mCardView);
            mCardView = null;
        }
    }

    //展示银行卡列表
    public void jsmethod_showListBackCard(UZModuleContext moduleContext) {

        mListItems = moduleContext.optJSONArray("listItems");
        mModuleContext = moduleContext;
        initDialg();
    }

    //隐藏银行卡列表
    public void jsmethod_hideListItem(UZModuleContext moduleContext) {

        if (mItemList != null && mItemList.isShowing()) {
            mItemList.hide();
        }
    }

    //弹出提现对话框
    public void jsmethod_withdrawCash(UZModuleContext moduleContext) {
        mModuleContext = moduleContext;
        String mIndex = moduleContext.optString("index", "");
        boolean mMoved = moduleContext.optBoolean("moved", true);
        if (mCash == null) {
            mCash = new WithdrawCash(getContext());
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.setMargins(0, 0, 0, 0);
            mCash.setBackgroundResource(R.drawable.backcardbg);
            insertViewToCurWindow(mCash, params, mIndex, mMoved);
        }
        cashMoney(moduleContext);
    }

    private void cashMoney(final UZModuleContext moduleContext) {
        String mBankIcon = moduleContext.optString("bankicon");
        final String mId = moduleContext.optString("mId", "-1");
        String mCardType = moduleContext.optString("cardtype");
        mListItems = moduleContext.optJSONArray("listItems");
        double mCashMeney = moduleContext.optDouble("balance", 0.00);
        //加载图片
        Picasso.with(getContext())
                .load(mBankIcon)
                //占位图，图片加载出来之前显示的默认图片
                .placeholder(R.drawable.loading)
                //错误图，图片加载出错时显示的图片
                .error(R.drawable.loading)
                .into(mCash.getCashIocn());

        //加载文字
        mCash.getCashType().setText(mCardType);

        //显示余额
        //可全部取出的前
        final double mlastMoney = mCashMeney - mCashMeney % 100;
        String mAllMoney = dealMeony(mCashMeney);
        String mLasMoney = dealMeony(mlastMoney);
        mCash.getBalanceMoney().setText("余额" + mAllMoney + "元，可提现金额" + mLasMoney + "元");
        //点击提现
        mCash.getHandLine().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initCashDialog();
            }
        });
        //点击全部提现
        mCash.getCashAll().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCash.getCashMoney().setText(mlastMoney + "");
            }
        });
        //开始提现
        mCash.getCashBingin().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String money = mCash.getCashMoney().getText().toString().trim();
                double chagerMoney = Double.parseDouble(money);
                if (chagerMoney % 100.0 != 0.0) {
                    ToastMaked.showToast(getContext(), "提现的钱数只能为100的倍数");
                    return;
                }
                JSONObject object = new JSONObject();
                try {
                    object.put("cashMoney", money);
                    object.put("mId", mId);
                    moduleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //处理金钱显示问题
    private String dealMeony(double money) {
        String mAllMoney = "0.00";
        String allMoney = money + "";
        if (allMoney.contains(".")) {
            int index = allMoney.indexOf(".");
            int length = allMoney.substring(index).length();
            if (length == 1) {
                mAllMoney = allMoney + "00";
            } else if (length == 2) {
                mAllMoney = allMoney + "0";
            } else {
                mAllMoney = allMoney;
            }
        } else {
            mAllMoney = allMoney + ".00";
        }
        return mAllMoney;
    }

    //隐藏提现对话框
    public void jsmethod_hidewithdrawCash(UZModuleContext moduleContext) {
        if (mCash != null) {
            removeViewFromCurWindow(mCash);
            mCash = null;
        }
    }

    //提现对话框dailoge
    public void jsmethod_openCashMoneyItems(UZModuleContext moduleContext) {
        mListItems = moduleContext.optJSONArray("listItems");
        mModuleContext = moduleContext;
        initCashDialog();
    }

    //隐藏绑卡列表
    public void jsmethod_closeCashMoneyItems(UZModuleContext moduleContext) {

        if (mItemList != null && mItemList.isShowing()) {
            mItemList.hide();
        }

    }

    private void initCashDialog() {
        if (mCashList == null) {
            mCashList = new ArrayList<>();
        } else {
            mCashList.clear();
        }
        if (mListItems != null && mListItems.length() != 0) {
            for (int i = 0; i < mListItems.length(); i++) {
                try {
                    String Json = (String) mListItems.get(i);
                    String newJson = Json.replace("'", "\"");
                    JSONObject jsonObject = new JSONObject(newJson);
                    String url = jsonObject.optString("url");
                    String text = jsonObject.optString("text");
                    String mId = jsonObject.optString("mId");
                    boolean select = jsonObject.optBoolean("select");
                    ItemsBeen been = new ItemsBeen();
                    been.setSelect(select);
                    been.setUrl(url);
                    been.setText(text);
                    been.setId(mId);
                    mCashList.add(been);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        if (mCashDialog == null) {
            mCashDialog = new Dialog(getContext());
            //去除标题栏
            mCashDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //2.填充布局
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mCashDialogView = inflater.inflate(R.layout.mo_naxiangbackcard_cash_items_layout, null);
            //将自定义布局设置进去
            mCashDialog.setContentView(mCashDialogView);

            //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = mCashDialog.getWindow();
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;

            //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
            mCashDialog.show();
            window.setAttributes(lp);
            //设置点击其它地方不让消失弹窗
            mCashDialog.setCancelable(true);

            //对话框消失的监听，主要监听回车键的监听
            mCashDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    JSONObject object = new JSONObject();
                    try {
                        object.put("dialogeDismiss", true);
                        mModuleContext.success(object, false);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });

        } else {
            mCashDialog.show();
        }


        //设置adapter适配器
        mCashCardItems = (ListView) mCashDialogView.findViewById(R.id.cashcarditems);

        if (mCashFootview == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mCashFootview = inflater.inflate(R.layout.mo_naxiangbackcard_foot_layout, null);
            mCashCardItems.addFooterView(mCashFootview);

        }
        mCashFootview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCashDialog.hide();
                JSONObject object = new JSONObject();
                try {
                    object.put("and", true);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        mCardItemAdapter = new CardItemAdapter(mCashList);
        mCashCardItems.setAdapter(mCardItemAdapter);

        //设置条目点击事件
        mCashCardItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                mCashDialog.hide();
                JSONObject object = new JSONObject();
                try {
                    object.put("mId", mCashList.get(position).getId());
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    //隐藏输入法
    public void jsmethod_hideKeyboard(UZModuleContext moduleContext) {
        EditText editText = new EditText(getContext());
        InputMethodManager imm = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        }
        ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getContext().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

        if (mKeyBardDialog == null) {
            mKeyBardDialog.hide();
        }

    }

    //自定义数字键盘
    public void jsmethod_openKeyboard(UZModuleContext moduleContext) {
        mModuleContext = moduleContext;
        mkeyItems = moduleContext.optJSONArray("keylist");
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (!imm.isActive()) {
            imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        }
        ((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getContext().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        if (mKeyBardDialog == null) {
            mKeyBardDialog = new Dialog(getContext(), R.style.keybard);
            //去除标题栏
            mKeyBardDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //2.填充布局
            LayoutInflater inflater = LayoutInflater.from(getContext());
            mKeyBardView = inflater.inflate(R.layout.mo_naxiangbackcard_keyboard_layout, null);
            //获取自定义控件
            mCloseKeybard = (RelativeLayout) mKeyBardView.findViewById(R.id.closekeybard);
            mOne = (Button) mKeyBardView.findViewById(R.id.one);
            mTwo = (Button) mKeyBardView.findViewById(R.id.two);
            mThree = (Button) mKeyBardView.findViewById(R.id.three);
            mFour = (Button) mKeyBardView.findViewById(R.id.four);
            mFive = (Button) mKeyBardView.findViewById(R.id.five);
            mSix = (Button) mKeyBardView.findViewById(R.id.six);
            mSeven = (Button) mKeyBardView.findViewById(R.id.seven);
            mEnghit = (Button) mKeyBardView.findViewById(R.id.enghit);
            mNine = (Button) mKeyBardView.findViewById(R.id.nine);
            mTen = (Button) mKeyBardView.findViewById(R.id.ten);
            mEleven = (Button) mKeyBardView.findViewById(R.id.eleven);
            mTwlelve = (LinearLayout) mKeyBardView.findViewById(R.id.twlelve);
            mDeleatImage = (ImageView) mKeyBardView.findViewById(R.id.deleatimage);

            //将自定义布局设置进去
            mKeyBardDialog.setContentView(mKeyBardView);
            //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = mKeyBardDialog.getWindow();
            //            window.setBackgroundDrawable(new ColorDrawable(0));//设置背景透明
            //            window.setContentView(R.layout.mo_naxiangbackcard_keyboard_layout);
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
            mKeyBardDialog.show();
            window.setAttributes(lp);
            //设置点击其它地方不让消失弹窗
            mKeyBardDialog.setCancelable(true);
        } else {
            mKeyBardDialog.show();
        }
        //对话框消失的监听，主要监听回车键的监听
        mKeyBardDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                JSONObject object = new JSONObject();
                try {
                    object.put("dialogeDismiss", true);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        if (mKey == null) {
            mKey = new ArrayList<>();
        } else {
            mKey.clear();
        }
        if (mkeyItems != null && mkeyItems.length() != 0) {
            for (int i = 0; i < mkeyItems.length(); i++) {
                try {
                    mKey.add((String) mkeyItems.get(i));
                    System.out.println((String) mkeyItems.get(i));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        if (mKey.size() == 11) {
            mOne.setText(mKey.get(0));
            mTwo.setText(mKey.get(1));
            mThree.setText(mKey.get(2));
            mFour.setText(mKey.get(3));
            mFive.setText(mKey.get(4));
            mSix.setText(mKey.get(5));
            mSeven.setText(mKey.get(6));
            mEnghit.setText(mKey.get(7));
            mNine.setText(mKey.get(8));
            mTen.setText(mKey.get(9));
            mEleven.setText(mKey.get(10));
        }
        final StringBuffer buffer = new StringBuffer();
        mCloseKeybard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mKeyBardDialog.dismiss();
                JSONObject object = new JSONObject();
                try {
                    object.put("dialogeDismiss", true);
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                buffer.reverse();
            }
        });


        mOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    buffer.append(mOne.getText().toString());
                    object.put("munber", mOne.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mOne.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mOne.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mOne.setTextColor(getContext().getResources().getColor(R.color.keytext));

                        break;
                }

                return false;
            }
        });
        mTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mTwo.getText().toString());
                    buffer.append(mTwo.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        mTwo.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mTwo.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mTwo.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mThree.getText().toString());
                    buffer.append(mThree.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mThree.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mThree.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mThree.setTextColor(getContext().getResources().getColor(R.color.keytext));

                        break;
                }
                return false;
            }
        });

        mFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mFour.getText().toString());
                    buffer.append(mFour.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mFour.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mFour.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mFour.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mFive.getText().toString());
                    buffer.append(mFive.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


        mFive.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mFive.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mFive.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mSix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mSix.getText().toString());
                    buffer.append(mSix.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mSix.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mSix.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mSix.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mSeven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mSeven.getText().toString());
                    buffer.append(mSeven.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mSeven.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mSeven.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mSeven.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });

        mEnghit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mEnghit.getText().toString());
                    buffer.append(mEnghit.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mEnghit.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mEnghit.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mEnghit.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mNine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mNine.getText().toString());
                    buffer.append(mNine.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        mNine.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mNine.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mNine.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });

        mTen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mTen.getText().toString());
                    buffer.append(mTen.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mTen.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mTen.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mTen.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mEleven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject object = new JSONObject();
                try {
                    object.put("munber", mEleven.getText().toString());
                    buffer.append(mEleven.getText().toString());
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        mEleven.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mEleven.setTextColor(getContext().getResources().getColor(R.color.keybard));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mEleven.setTextColor(getContext().getResources().getColor(R.color.keytext));
                        break;
                }
                return false;
            }
        });
        mTwlelve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.removeCallbacksAndMessages(null);
                JSONObject object = new JSONObject();
                try {

                    if (buffer.length() != 0) {
                        buffer.deleteCharAt(buffer.toString().length() - 1);
                    }
                    object.put("allmunber", buffer);
                    mModuleContext.success(object, false);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return;

            }
        });
        mTwlelve.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (buffer.length() != 0) {
                            buffer.deleteCharAt(buffer.toString().length() - 1);
                        } else {
                            mHandler.removeCallbacksAndMessages(null);
                            return;
                        }

                        JSONObject object = new JSONObject();
                        try {
                            object.put("allmunber", buffer);
                            mModuleContext.success(object, false);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        mHandler.postDelayed(this, 100);
                    }
                }, 100);

                return false;
            }
        });
        mTwlelve.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mDeleatImage.setBackgroundResource(R.drawable.undelelate);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        mDeleatImage.setBackgroundResource(R.drawable.delelate);
                        break;
                }
                return false;
            }
        });
    }


}


